package com.moxiao.yfjh.module.blind.dal.mysql.sessionRecord;

import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.framework.mybatis.core.mapper.BaseMapperX;
import com.moxiao.yfjh.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.moxiao.yfjh.module.blind.controller.admin.sessionRecord.vo.SessionRecordExportReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.sessionRecord.vo.SessionRecordPageReqVO;
import com.moxiao.yfjh.module.blind.dal.dataobject.member.BlindMemberUserDO;
import com.moxiao.yfjh.module.blind.dal.dataobject.sessionRecord.SessionRecordDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Objects;

/**
 * 相亲会话记录 Mapper
 *
 * @author mx
 */
@Mapper
public interface SessionRecordMapper extends BaseMapperX<SessionRecordDO> {

    default PageResult<SessionRecordDO> selectPage(SessionRecordPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<SessionRecordDO>()
                .eqIfPresent(SessionRecordDO::getNo, reqVO.getNo())
                .eqIfPresent(SessionRecordDO::getRecordId, reqVO.getRecordId())
                .eqIfPresent(SessionRecordDO::getFounder, reqVO.getFounder())
                .eqIfPresent(SessionRecordDO::getParticipants, reqVO.getParticipants())
                .eqIfPresent(SessionRecordDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(SessionRecordDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(SessionRecordDO::getId));
    }

    default PageResult<SessionRecordDO> selectPageByUserId(SessionRecordPageReqVO reqVO) {

        if (null == reqVO.getUserId()) {
            return selectPage(reqVO, new LambdaQueryWrapperX<SessionRecordDO>()
                    .eqIfPresent(SessionRecordDO::getNo, reqVO.getNo())
                    .eqIfPresent(SessionRecordDO::getRecordId, reqVO.getRecordId())
                    .eqIfPresent(SessionRecordDO::getFounder, reqVO.getFounder())
                    .eqIfPresent(SessionRecordDO::getParticipants, reqVO.getParticipants())
                    .eqIfPresent(SessionRecordDO::getStatus, reqVO.getStatus())
                    .betweenIfPresent(SessionRecordDO::getCreateTime, reqVO.getCreateTime())
                    .orderByDesc(SessionRecordDO::getId));
        } else {
            return selectPage(reqVO, new LambdaQueryWrapperX<SessionRecordDO>()
                    .eqIfPresent(SessionRecordDO::getFounder, reqVO.getUserId())
                    .or().eq(SessionRecordDO::getParticipants, reqVO.getUserId())
                    .orderByDesc(SessionRecordDO::getId));
        }
    }

    default List<SessionRecordDO> selectList(SessionRecordExportReqVO reqVO) {
        LambdaQueryWrapperX<SessionRecordDO> sessionRecordDOLambdaQueryWrapperX = new LambdaQueryWrapperX<>();
        sessionRecordDOLambdaQueryWrapperX.eqIfPresent(SessionRecordDO::getNo, reqVO.getNo());
        sessionRecordDOLambdaQueryWrapperX.eqIfPresent(SessionRecordDO::getRecordId, reqVO.getRecordId());
        sessionRecordDOLambdaQueryWrapperX.eqIfPresent(SessionRecordDO::getFounder, reqVO.getFounder());
        sessionRecordDOLambdaQueryWrapperX.eqIfPresent(SessionRecordDO::getParticipants, reqVO.getParticipants());
        sessionRecordDOLambdaQueryWrapperX.eqIfPresent(SessionRecordDO::getStatus, reqVO.getStatus());
        sessionRecordDOLambdaQueryWrapperX.betweenIfPresent(SessionRecordDO::getCreateTime, reqVO.getCreateTime());
        sessionRecordDOLambdaQueryWrapperX.orderByDesc(SessionRecordDO::getId);
        return selectList(sessionRecordDOLambdaQueryWrapperX);
    }

    @Select("select *  from blind_session_record where (founder=#{founder} and participants=#{participants}) || (founder=#{participants} and participants=#{founder})")
    List<SessionRecordDO> selectRecordList(@Param("founder") String founder,@Param("participants") String participants);

    @Select("select count(*) num from blind_session_record where no = #{sessionNo}")
    Integer getSessionRecordNo(@Param("sessionNo") String sessionNo);

}
